package com.holly.unit.bpmn.activiti.ext;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.holly.unit.bpmn.activiti.entity.ActButton;
import com.holly.unit.bpmn.activiti.mapper.ActivitiZButtonMapper;
import com.holly.unit.bpmn.activiti.pojo.ActButtonRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 类描述: 流程业务扩展实现类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/2/22 11:02
 */
@Service
public class ActivitiZButtonService extends ServiceImpl<ActivitiZButtonMapper, ActButton> implements IService<ActButton> {

    public void addButton(ActButtonRequest actButtonRequest) {
        try {
            QueryWrapper<ActButton> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("proc_def_id", actButtonRequest.getProcDefId());
            queryWrapper.eq("node_id", actButtonRequest.getNodeId());
            this.baseMapper.delete(queryWrapper);
            List<ActButton> list = new ArrayList<>();
            for (Object ite : actButtonRequest.getButtonArry()) {
                JSONObject button = JSONObject.parseObject(JSONObject.toJSONString(ite));
                ActButton actButton = new ActButton();
                actButton.setProcDefId(actButtonRequest.getProcDefId());
                actButton.setNodeId(actButtonRequest.getNodeId());
                actButton.setButtonCode(button.getString("buttonCode"));
                actButton.setButtonType(button.getInteger("buttonType"));
                actButton.setButtonName(button.getString("buttonName"));
                list.add(actButton);
            };
            this.saveBatch(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<ActButton> getFromPage(ActButtonRequest actButtonRequest) {
        try {
            QueryWrapper<ActButton> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("proc_def_id", actButtonRequest.getProcDefId());
            queryWrapper.eq("node_id", actButtonRequest.getNodeId());
            return this.baseMapper.selectList(queryWrapper);
        } catch (Exception e) {
            // 业务表需要有 act_status字段，没有会报错，不管他
            e.printStackTrace();
        }
        return  null;
    }

}
